package com.bus.admin.controller;


import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.bus.admin.bean.AdminUser;
import com.bus.admin.bean.result.ResultPage;
import com.bus.admin.controller.helper.$;
import com.bus.admin.controller.model.ApiResultModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-09-03
 */
@Controller
@RequestMapping("/commen/user")
@Slf4j
public class AdminUserController extends BaseController {
     /** 主页面 */
    @RequestMapping("main.html")
    public String main(){
       return  "/user/UserMain";
    }
    /** 添加页面跳转
     * @return 返回页面
     */
    @RequestMapping(value = "/addUserHtml")
    public String addUserHtml( ){
             return "/user/UserAdd";
    }
    /** 编辑页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "/editUserHtml")
    public String editUserHtml(Integer id ,Model model ){
           model.addAttribute("user",adminUserService.selectUserByIdUser(id));
             return "/user/UserEdit";
    }

    /** 查看页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "/showUserHtml")
    public String showUserHtml(Integer id ,Model model ){
           model.addAttribute("user",adminUserService.selectUserByIdUser(id));
             return "/user/UserShow";
    }


    /** 分页查询User
     * @param page  页码
     * @param limit  查几条
     * @param user  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "/findUserByPage")
    @ResponseBody
    public ResultPage findUserByPage(Integer page, Integer limit, AdminUser user){
             return adminUserService.findUserByPage(page,limit,user);
    }

    /** 校验当前用户密码
     * @param page  页码
     * @param limit  查几条
     * @param user  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "/isPass")
    @ResponseBody
    public ApiResultModel isPass(String pass){
            if( getUser().getPassWord().equals(pass)){
                return $.success();
            }else{
                return $.success("密码错误");
            }
    }

    /** 添加User
     * @param user  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/addUser")
    @ResponseBody
    public ApiResultModel saveUser(AdminUser user   ){
        if(!Validator.isEmail(user.getEmail())){
             return $.error("邮箱格式错误");
        }
    /*    if(StrUtil.hasBlank(code)){
            return $.error("邮箱授权码不能为空");
        }*/
       //String cacheCode= getJSONCache(user.getEmail());
            return adminUserService.addUser(user );
    }



    /** 编辑User
     * @param user  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/editUser")
    @ResponseBody
    public ApiResultModel editUser(AdminUser user ){
             return adminUserService.editUser(user );
    }


    /** 修改User密码
     * @param user  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/editPass")
    @ResponseBody
    public ApiResultModel editPass(AdminUser user  ,String code){
        if(!Validator.isEmail(user.getEmail())){
            return $.error("邮箱格式错误");
        }
        if(StrUtil.hasBlank(code)){
            return $.error("邮箱授权码不能为空");
        }
        String cacheCode= getJSONCache(user.getEmail());
        if(StrUtil.equalsIgnoreCase(cacheCode,code)){
            return adminUserService.editPass(user,code);
        }else{
            return $.error("邮箱授权码不匹配");
        }

    }

    /** 角色授权页面
     * @param id  角色ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "/userAuthorization.html")
    public String userAuthorizationHtml(Integer id,Model model ){
        model.addAttribute("user",adminUserService.findUserByUserId(id));
             return "user/UserAuthorization";
    }



    /** 删除一条User
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "/delUser")
    @ResponseBody
    public ApiResultModel delUser( Integer id  ){
             return adminUserService.delUser(id);
    }



    /** 登陆  */
    @RequestMapping(value = "/login")
    @ResponseBody
    public ApiResultModel login(String account, String passWord , HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(
                account,
                passWord);
        //进行验证，这里可以捕获异常，然后返回对应信息
        try{
            subject.login(token);
        }catch (Exception e){
            e.printStackTrace();
            return $.error("登陆失败，请检查用户名密码");
        }
        request.setAttribute("user",getUser());
        log.debug(subject.getPrincipal().toString());
        return $.success();
    }

    @RequestMapping(value = "/logout.html")
    public String logout( ){
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.logout();
        }catch (Exception e ){

        }

        return "/login.html";
    }


}
